package MyGui;

import javax.swing.*;
import DistSys.*;

class BooleanVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        String msg = "Please input 1 or 0!";
        try{
            int i = Integer.parseInt(tf.getText());
            if(i==0 || i==1)  return true;
            else {
                JOptionPane.showMessageDialog(input, msg);
                return false;
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input, msg);
            return false;
        }
     }
}

class IntVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        String msg = "Please input an integer!";
        try{
            int i = Integer.parseInt(tf.getText());
            return true;
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input, msg );
            return false;
        }
     }
}

class PosAndZeroIntVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        String msg = "Please input an integer >=0!";
        try{
            int i = Integer.parseInt(tf.getText());
            if(i>=0){ return true; }
            else {
                JOptionPane.showMessageDialog(input, msg);
                return false;
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input, msg);
            return false;
        }
     }
}

class PosIntVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        String msg = "Please input a positive integer!";
        try{
            int i = Integer.parseInt(tf.getText());
            if(i>0){ return true; }
            else {
                JOptionPane.showMessageDialog(input, msg);
                return false;
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input, msg);
            return false;
        }
     }
}

class DoubleVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        String msg = "Please input a number!";
        try{
            double d = Double.parseDouble(tf.getText());
            return true;
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input, msg);
            return false;
        }
     }
}

class PowerFactorVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        try{
            double d = Double.parseDouble(tf.getText());
            if( d>=0.0 && d<=1.0) { return true; }
            else {
                JOptionPane.showMessageDialog(input,
                            "Power factor should be in the range of [0,1]!");
                return false;
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input,"Please input a number!");
            return false;
        }
     }
}

class LayerVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        try{
            int val = Integer.parseInt( tf.getText() );
            if( val>=0 && val < Sys.prjInfo.NUM_FORE_GND_LAYER) { return true; }
            else {
                JOptionPane.showMessageDialog(input,
                            "Layer should be in the range of [0,"+
                            (Sys.prjInfo.NUM_FORE_GND_LAYER-1)+" ]!");
                return false;
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(input,"Please input a number!");
            return false;
        }
     }
}